package xyz.ljcc.ccmt.service.impl.admin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import xyz.ljcc.ccmt.commons.utils.UpdateTool;
import xyz.ljcc.ccmt.model.base.Role;
import xyz.ljcc.ccmt.model.base.RoleVsOperation;
import xyz.ljcc.ccmt.repository.admin.RoleRepository;
import xyz.ljcc.ccmt.repository.admin.RoleVsOperationReposity;
import xyz.ljcc.ccmt.service.api.admin.RoleService;

@Service
public class RoleServiceImpl implements RoleService {

	@Autowired
	RoleRepository roleRepository;
	
	@Autowired
	RoleVsOperationReposity roleVsOperationReposity;
	
	@Override
	public String getRolesByLoginName(String loginName) {
		List<Role> roles = roleRepository.loadByLoginName(loginName);
		if(roles.size() > 0) {
			StringBuilder roleStr = new StringBuilder();
			int ii = 0;
			for (Role role : roles) {
				roleStr.append(ii++ == 0 ? role.getRoleName() : "," + role.getRoleName());
			}
			return roleStr.toString();
		}else {
			return "护士";
		}
	}

	@Override
	public Role saveRole(Role role) {
		role.setActive(1);
		return roleRepository.save(role);
	}

	@Override
	public Role editRole(Role role) {
		Role role2 = roleRepository.findOne(role.getId());
		UpdateTool.copyNullProperties(role2, role);
		return roleRepository.save(role);
	}

	@Override
	public void delRole(Integer id) {
		roleRepository.delete(id);
	}

	@Override
	public Role getRole(Integer id) {
		return roleRepository.findOne(id);
	}

	@Override
	public void activeRole(Integer id, Integer active) {
		roleRepository.updateByHql(" update Role r set r.active = ?1 where r.id = ?2 ", active, id);
	}

	@Override
	public List<Role> loadRoleList(String roleName) {
		List<Role> roles = null;
		if(StringUtils.isEmpty(roleName.trim()))
			roles = roleRepository.findAll();
		else
			roles = roleRepository.getByRoleNameContaining(roleName);
		return roles;
	}

	@Override
	public Map<String, Object> getPermission(Integer roleId) {
		Map<String, Object> map = new HashMap<>();
		List<RoleVsOperation> roleVsModules = roleVsOperationReposity.getByRoleId(roleId);
		map.put("roleVsModules", roleVsModules);
		return map;
	}

	@Override
	public void savePermission(Integer roleId, String modules) {
		List<RoleVsOperation> roleVsModules = roleVsOperationReposity.getByRoleId(roleId);
		List<Integer> moduleStrList = new ArrayList<>();
		// 删除
		for (RoleVsOperation roleVsModule : roleVsModules) { // 科室
			if (!modules.contains(roleVsModule.getOperationId() + ",")) { // 传入的里面不包含当前，表示这个不需要了
				roleVsOperationReposity.delete(roleVsModule);
			}
			moduleStrList.add(roleVsModule.getOperationId());
		}
		// 新增
		for (String operationId : modules.split(",")) {
			if(StringUtils.isEmpty(operationId))
				continue;
			if (!moduleStrList.contains(Integer.parseInt(operationId))) {
				RoleVsOperation roleVsModule = new RoleVsOperation();
				roleVsModule.setOperationId(Integer.parseInt(operationId));
				roleVsModule.setRoleId(roleId);
				roleVsOperationReposity.save(roleVsModule);
			}
		}
	}

}
